/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trefliq.EJB.sessionbeans;

import java.util.List;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import trefliq.EJB.entities.Credential;

/**
 *
 * @author Yash
 */
@Stateless
@PermitAll // (by default, allow all, restrict what's necessary)
public class CredentialSession extends AbstractFacade<Credential> {
    @PersistenceContext(unitName = "trefliqPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CredentialSession() {
        super(Credential.class);
    }

    public boolean createCredential(String username, String password) {
        try{
            Credential c = new Credential(username, password);
            em.persist(c);
        }catch(Exception e){
            System.err.println("Cound not create credential: "+e); // Can be logged alternatively.
            return false;
        }
        return true;
    }
    

    // AbstractFacade methods.  Copied in to apply security attributes.
    
    @Override
    public void create(Credential entity) {
        super.create(entity);
    }

    @Override
    public void edit(Credential entity) {
        super.edit(entity);
    }

    @Override
    public void remove(Credential entity) {
        super.remove(entity);
    }

    @Override
    public Credential find(Object id) {
        return super.find(id);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public List<Credential> findAll() {
        return super.findAll();
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public List<Credential> findRange(int[] range) {
        return super.findRange(range);
    }

    @Override
    //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
    public int count() {
        return super.count();
    }

    
}
